#include "handlemembercard.h"
#include "ui_handlemembercard.h"
#include "inquiresql.h"
#include <QKeyEvent>
#include <QSound>
#include <QDir>
#include <QMessageBox>
#include "mainwindow.h"
#include <QList>
#include "calendar.h"
#include <QDateTime>
#include "setmemberlevel.h"
#include "personmemberoperationdetails.h"

#include <QDesktopWidget>

class MainWindow;

HandleMemberCard::HandleMemberCard(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::HandleMemberCard)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(492,571);
    setWindowTitle(tr("会员卡办理"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;
    m_calendar = new Calendar(this);
    m_calendar->hide();
    m_setMemberLevel = new SetMemberLevel;

    //正则表达式语句中不能乱加空格
    QRegExp regExpDiscount("[0-9]{1,2}|100");
    QRegExpValidator *pRegExpValidatorDiscount = new QRegExpValidator(regExpDiscount, this);
    ui->lineEditDiscount->setValidator(pRegExpValidatorDiscount);

    QRegExp regExpPrice(" [0-9]{0,6}|[0-9]{0,6}[.]{1}[0-9]{0,2} ");
    QRegExpValidator *pRegExpValidatorPrice = new QRegExpValidator(regExpPrice, this);
    ui->lineEditMemberCardAmount->setValidator(pRegExpValidatorPrice);
    ui->lineEditCashPayment->setValidator(pRegExpValidatorPrice);
    ui->lineEditCUPPayment->setValidator(pRegExpValidatorPrice);

    QRegExp regExpIntegration("[0-9]{1,5}");
    QRegExpValidator *pRegExpValidatorIntegration = new QRegExpValidator(regExpIntegration, this);
    ui->lineEditIntegration->setValidator(pRegExpValidatorIntegration);

    //匹配所有合理年月日的正则表达式： ^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$
    QRegExp regExpHandleDate("^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])"
                            "|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]"
                            "|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$");
    QRegExpValidator *pRegExpValidatorHandleDate = new QRegExpValidator(regExpHandleDate, this);
    ui->lineEditHandleDate->setValidator(pRegExpValidatorHandleDate);

    ui->lineEditHandleDate->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));

    //初始化QComboBox
    slotUpdateMemberLevel();

    eliminationPushButtonBorder();
    initMemberInformation();

    connect(m_calendar, SIGNAL(signalUpdateLineEditDate(QString)),
            this, SLOT(slotUpdatelineEditHandleDate(QString)));
    connect(m_setMemberLevel, SIGNAL(updateMemberLevel()), this, SLOT(slotUpdateMemberLevel()));

    //添加时间过滤器
    ui->lineEditMemberCardNumber->installEventFilter(this);
    ui->lineEditMemberName->installEventFilter(this);
    ui->comboBoxGender->installEventFilter(this);
    ui->lineEditTel->installEventFilter(this);
    ui->lineEditEmail->installEventFilter(this);
    ui->lineEditAdress->installEventFilter(this);
    ui->lineEditHandleDate->installEventFilter(this);
    ui->comboBoxMemberLevel->installEventFilter(this);
    ui->lineEditIntegration->installEventFilter(this);
    ui->lineEditDiscount->installEventFilter(this);
    ui->lineEditMemberCardAmount->installEventFilter(this);
    ui->comboBoxStaffName->installEventFilter(this);
    ui->lineEditMemberPassword->installEventFilter(this);
    ui->lineEditConfirmPassword->installEventFilter(this);
    ui->lineEditCashPayment->installEventFilter(this);
    ui->lineEditCUPPayment->installEventFilter(this);
    ui->textEditRemark->installEventFilter(this);
}

HandleMemberCard::~HandleMemberCard()
{
    delete ui;
}

void HandleMemberCard::set_staffName()
{
    const QStringList staffNameList = m_sql->inquireAllStaffName();
    ui->comboBoxStaffName->addItems(staffNameList);
    QString currentStaffName = m_sql->InquireStaffName(MainWindow::staffID);
    for (int i = 0; i < staffNameList.count(); ++i)
    {
        if (ui->comboBoxStaffName->itemText(i) == currentStaffName)
        {
            ui->comboBoxStaffName->setCurrentIndex(i);
            break;
        }
    }
}

bool HandleMemberCard::eventFilter(QObject *target, QEvent *event)
{
    if (target == ui->lineEditMemberCardNumber || target == ui->lineEditMemberName
            || target == ui->comboBoxGender || target == ui->lineEditTel
            || target == ui->lineEditEmail || target == ui->lineEditAdress
            || target == ui->lineEditHandleDate || target == ui->comboBoxMemberLevel
            || target == ui->lineEditIntegration || target == ui->lineEditDiscount
            || target == ui->lineEditMemberCardAmount || target == ui->comboBoxStaffName
            || target == ui->lineEditMemberPassword || target == ui->lineEditConfirmPassword
            || target == ui->lineEditCashPayment || target == ui->lineEditCUPPayment
            || target == ui->textEditRemark)
    {
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditMemberCardNumber)
                {
                    if (m_lastClick != lineEditMemberCardNumber)
                    {
                        ui->lineEditMemberCardNumber->selectAll();
                        event->accept();
                        m_lastClick = lineEditMemberCardNumber;
                        return true;
                    }
                    else if (m_lastClick == lineEditMemberCardNumber)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditMemberName)
                {
                    if (m_lastClick != lineEditMemberName){
                        ui->lineEditMemberName->selectAll();
//                        event->accept();
                        m_lastClick = lineEditMemberName;
                        return true;
                    }else if (m_lastClick == lineEditMemberName)
                    {
                        goto flag;
                    }
                }
                if (target == ui->comboBoxGender)
                {
                    ui->comboBoxGender->setCurrentIndex(-1);
//                    if (m_lastClick != comboBoxGender){
//                        m_lastClick = comboBoxGender;
//                        return true;
//                    }else if (m_lastClick == comboBoxGender)
//                    {
//                        goto flag;
//                    }
                }
                if (target == ui->lineEditTel)
                {
                    if (m_lastClick != lineEditTel){
                        m_lastClick = lineEditTel;
                        ui->lineEditTel->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditTel)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditEmail)
                {
                    if (m_lastClick != lineEditEmail){
                        m_lastClick = lineEditEmail;
                        ui->lineEditEmail->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditEmail)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditAdress)
                {
                    if (m_lastClick != lineEditAdress){
                        m_lastClick = lineEditAdress;
                        ui->lineEditAdress->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditAdress)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditHandleDate)
                {
                    if (m_lastClick != lineEditHandleDate){
                        m_lastClick = lineEditHandleDate;
                        ui->lineEditHandleDate->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditHandleDate)
                    {
                        goto flag;
                    }
                }
                if (target == ui->comboBoxMemberLevel)
                {
                    ui->comboBoxMemberLevel->setCurrentIndex(-1);
                }
                if (target == ui->lineEditIntegration)
                {
                    if (m_lastClick != lineEditIntegration){
                        m_lastClick = lineEditIntegration;
                        ui->lineEditIntegration->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditIntegration)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditDiscount)
                {
                    if (m_lastClick != lineEditDiscount){
                        m_lastClick = lineEditDiscount;
                        ui->lineEditDiscount->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditDiscount)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditMemberCardAmount)
                {
                    if (m_lastClick != lineEditMemberCardAmount){
                        m_lastClick = lineEditMemberCardAmount;
                        ui->lineEditMemberCardAmount->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditMemberCardAmount)
                    {
                        goto flag;
                    }
                }
                if (target == ui->comboBoxStaffName)
                {
                    ui->comboBoxStaffName->setCurrentIndex(-1);
                }
                if (target == ui->lineEditMemberPassword)
                {
//                    EnterPass.wav
//                    QString dir=QCoreApplication::applicationDirPath();
//                    QString filename(dir+"/sound/EnterPass.wav");
//                    QSound::play ( filename );

//                    QDir temDir("./sound/EnterPass.wav");
//                    QString absDir = temDir.absolutePath();
//                    QSound bells(absDir);
//                    bells.play();
//                    qDebug() << absDir;
//                    QSound::isAvailable();
                    //QSound::play("./sound/EnterPass.wav");

                    if (m_lastClick != lineEditMemberPassword){
                        m_lastClick = lineEditMemberPassword;
                        ui->lineEditMemberPassword->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditMemberPassword)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditConfirmPassword)
                {
                    if (m_lastClick != lineEditConfirmPassword){
                        m_lastClick = lineEditConfirmPassword;
                        ui->lineEditConfirmPassword->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditConfirmPassword)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditCashPayment)
                {
                    if (m_lastClick != lineEditCashPayment){
                        m_lastClick = lineEditCashPayment;
                        if (ui->lineEditMemberCardAmount->text() != "0.00")
                        {
                            ui->lineEditCashPayment->setText(
                            completionPrice(QString::number(ui->lineEditMemberCardAmount->text().toDouble()
                            - ui->lineEditCUPPayment->text().toDouble())));
                        }
                        ui->lineEditCashPayment->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditCashPayment)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditCUPPayment)
                {
                    if (m_lastClick != lineEditCUPPayment){
                        m_lastClick = lineEditCUPPayment;
                        if (ui->lineEditMemberCardAmount->text() != "0.00")
                        {
                            ui->lineEditCUPPayment->setText(
                                        completionPrice(QString::number(ui->lineEditMemberCardAmount->text().toDouble()
                                        - ui->lineEditCashPayment->text().toDouble())));
                        }
                        ui->lineEditCUPPayment->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditCUPPayment)
                    {
                        goto flag;
                    }
                }
                if (target == ui->textEditRemark)
                {
                    if (m_lastClick != textEditRemark){
                        m_lastClick = textEditRemark;
                        ui->textEditRemark->selectAll();
                        return true;
                    }else if (m_lastClick == textEditRemark)
                    {
                        goto flag;
                    }
                }
            }
        }

        //鼠标左键释放事件
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditHandleDate)
                {
                    m_calendar->move((QApplication::desktop()->width() - m_calendar->width())/2,
                                     (QApplication::desktop()->height() - m_calendar->height())/2);
                    m_calendar->setSelectedCurrentDate(ui->lineEditHandleDate->text());
                    m_calendar->setWindowFlags(
                                Qt::Dialog | Qt::FramelessWindowHint);

                    QPoint GlobalPoint(ui->lineEditHandleDate->mapToGlobal(QPoint(0, 0)));
                    int x = GlobalPoint.x();
                    int y = GlobalPoint.y();
                    m_calendar->setGeometry(x , y + 35, 150, 130);

                    //在对 QWidget 操作时，不能操作其他东西
//                    m_calendar->setWindowModality(Qt::ApplicationModal);
//                    m_calendar->setAttribute(Qt::WA_ShowModal, true);
                    m_calendar->show();
                    goto flag;
                }
            }
//            goto flag;
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditMemberCardNumber)
                {
                    ui->lineEditMemberName->setFocus();
                    ui->lineEditMemberName->selectAll();
                    return true;
                }
                if (target == ui->lineEditMemberName)
                {
                    ui->comboBoxGender->setFocus();
                    ui->comboBoxGender->showPopup();
                    return true;
                }
                if (target == ui->comboBoxGender)
                {
                    ui->lineEditTel->setFocus();
                    ui->lineEditTel->selectAll();
                    return true;
                }
                if (target == ui->lineEditTel)
                {
                    ui->lineEditEmail->setFocus();
                    ui->lineEditEmail->selectAll();
                    return true;
                }
                if (target == ui->lineEditEmail)
                {
                    ui->lineEditAdress->setFocus();
                    ui->lineEditAdress->selectAll();
                    return true;
                }
                if (target == ui->lineEditAdress)
                {
                    ui->lineEditHandleDate->setFocus();
                    ui->lineEditHandleDate->selectAll();
                    return true;
                }
                if (target == ui->lineEditHandleDate)
                {
                    ui->comboBoxMemberLevel->setFocus();
                    ui->comboBoxMemberLevel->showPopup();
                    return true;
                }
                if (target == ui->comboBoxMemberLevel)
                {
                    ui->lineEditIntegration->setFocus();
                    ui->lineEditIntegration->selectAll();
                    return true;
                }
                if (target == ui->lineEditIntegration)
                {
                    ui->lineEditDiscount->setFocus();
                    ui->lineEditDiscount->selectAll();
                    return true;
                }
                if (target == ui->lineEditDiscount)
                {
                    ui->lineEditMemberCardAmount->setFocus();
                    ui->lineEditMemberCardAmount->selectAll();
                    return true;
                }
                if (target == ui->lineEditMemberCardAmount)
                {
                    ui->comboBoxStaffName->setFocus();
                    ui->comboBoxStaffName->showPopup();
                    return true;
                }
                if (target == ui->comboBoxStaffName)
                {
                    ui->lineEditMemberPassword->setFocus();
                    ui->lineEditMemberPassword->selectAll();

//                    QDir temDir("./sound/EnterPass.wav");
//                    QString absDir = temDir.absolutePath();
//                    QSound bells(absDir);
//                    bells.play();
//                    if (bells.isAvailable() == false)
//                    {
//                        qDebug() << "false";
//                    }
                    return true;
                }
                if (target == ui->lineEditMemberPassword)
                {
                    ui->lineEditConfirmPassword->setFocus();
                    ui->lineEditConfirmPassword->selectAll();
                    return true;
                }
                if (target == ui->lineEditConfirmPassword)
                {
                    ui->lineEditCashPayment->setFocus();
                    ui->lineEditCashPayment->selectAll();
                    return true;
                }
                if (target == ui->lineEditCashPayment)
                {
                    ui->lineEditCUPPayment->setFocus();
                    if (ui->lineEditMemberCardAmount->text() != "0.00")
                    {
                        ui->lineEditCUPPayment->setText(
                                    completionPrice(QString::number(ui->lineEditMemberCardAmount->text().toDouble()
                                    - ui->lineEditCashPayment->text().toDouble())));
                    }
                    ui->lineEditCUPPayment->selectAll();
                    return true;
                }
                if (target == ui->lineEditCUPPayment)
                {
                    ui->textEditRemark->setFocus();
                    ui->textEditRemark->selectAll();
                    return true;
                }
                if (target == ui->textEditRemark)
                {
                    ui->pushButtonSave->setFocus();
                    return true;
                }
                if (target == ui->pushButtonSave)
                {
//                    if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
                    on_pushButtonSave_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void HandleMemberCard::on_pushButtonCancel_clicked()
{
    this->hide();
}

void HandleMemberCard::on_pushButtonSave_clicked()
{    
    if (ui->lineEditMemberCardNumber->text().isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n请输入会员卡号") );
        ui->lineEditMemberCardNumber->setFocus();
        return;
    }
    if (ui->lineEditMemberName->text().isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n请输入会员姓名") );
        ui->lineEditMemberName->setFocus();
        return;
    }
    if (ui->comboBoxGender->currentIndex() == -1)
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n请输入会员性别") );
        ui->comboBoxGender->setFocus();
        return;
    }
    if (ui->lineEditTel->text().isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n请输入联系电话") );
        ui->lineEditTel->setFocus();
        return;
    }
    if (ui->lineEditMemberPassword->text().isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n请输入会员密码") );
        ui->lineEditMemberPassword->setFocus();
        return;
    }
    if (ui->lineEditConfirmPassword->text().isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n请输入确认密码") );
        ui->lineEditConfirmPassword->setFocus();
        return;
    }
    if (ui->lineEditConfirmPassword->text() != ui->lineEditMemberPassword->text())
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n新设密码与确认密码不一致,请核对!"));
        ui->lineEditConfirmPassword->setFocus();
        return;
    }
    //插入会员信息
    PersonMember memberInformation(ui->lineEditMemberCardNumber->text(),
          ui->lineEditMemberName->text(), ui->comboBoxGender->currentText(),
          ui->lineEditTel->text(), ui->lineEditEmail->text(),
          ui->lineEditAdress->text(), ui->lineEditHandleDate->text(),
          ui->comboBoxMemberLevel->currentText(), ui->lineEditIntegration->text(),
          ui->lineEditDiscount->text(), ui->lineEditMemberCardAmount->text(),
          ui->comboBoxStaffName->currentText(), ui->lineEditConfirmPassword->text(),
          ui->textEditRemark->toPlainText(), ui->lineEditCashPayment->text().toDouble(),
          ui->lineEditCUPPayment->text().toDouble());
    if (!m_sql->InsertMemberInformation(memberInformation))
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n会员卡办理失败!"));
        return;
    }

    if (!m_sql->UpdatStaffMemberRecharge(ui->comboBoxStaffName->currentText(),
                                      ui->lineEditMemberCardAmount->text()))
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."), tr("\n更新员工业绩失败") );
        return;
    }
    //插入会员办理明细
//    PersonMemberOperationDetails(const QString memberID, const QString memberName,
//                 const QString memberLevel, const QString consumptionType,
//                 const QString consumptionTotal, const QString integration,
//                 const QString cashPayment, const QString cupPayment,
//                 const QString staffName, const QString consumptionDate)

    PersonMemberOperationDetails memberOperationDetails(
        ui->lineEditMemberCardNumber->text(), ui->lineEditMemberName->text(),
        ui->comboBoxMemberLevel->currentText(), tr("会员办卡"),
        ui->lineEditMemberCardAmount->text(), ui->lineEditIntegration->text(),
        ui->lineEditCashPayment->text(), ui->lineEditCUPPayment->text(),
        ui->comboBoxStaffName->currentText(), ui->lineEditHandleDate->text());
    if (!m_sql->InsertMemberOperationDetails(memberOperationDetails))
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."), tr("\n插入会员操作明细失败") );
        return;
    }
    else
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."), tr("\n会员办卡成功") );
        initMemberInformation();
    }
//flag:
//    initMemberInformation();
}

void HandleMemberCard::on_pushButtonAutoAddMemberCardNumber_clicked()
{
    ui->lineEditMemberCardNumber->setText(m_autoAddMemberCardNumber);
}

void HandleMemberCard::eliminationPushButtonBorder()
{
    //消除边框
    ui->pushButtonAddMemberLevel->setFlat(true);
    ui->pushButtonAutoAddMemberCardNumber->setFlat(true);
    ui->pushButtonCancel->setFlat(true);
    ui->pushButtonSave->setFlat(true);
}

void HandleMemberCard::initMemberInformation()
{
    initMemberLevel();

    ui->comboBoxMemberLevel->setCurrentIndex(-1);
    ui->comboBoxGender->setCurrentIndex(-1);

    ui->lineEditMemberCardNumber->setText(NULL);
    ui->lineEditMemberName->setText(NULL);
    ui->lineEditTel->setText(NULL);
    ui->lineEditEmail->setText(NULL);
    ui->lineEditAdress->setText(NULL);
    ui->lineEditConfirmPassword->setText(NULL);
    ui->lineEditMemberPassword->setText(NULL);
    ui->textEditRemark->setText(NULL);
}

void HandleMemberCard::initMemberLevel()
{
    ui->lineEditIntegration->setText("0");
    ui->lineEditDiscount->setText("100");
    ui->lineEditMemberCardAmount->setText("0.00");
    ui->lineEditCashPayment->setText("0.00");
    ui->lineEditCUPPayment->setText("0.00");
}

QString HandleMemberCard::completionPrice(const QString &price)
{
    QString number = price;
    if (number.isEmpty() || number.toDouble() == 0)
    {
        number.append("0.00");
        return number;
    }
    if (number.contains(".", Qt::CaseInsensitive))
    {
        if (number.left(1) == ".")
        {
            number.replace(0, 1, "0.");
            return number;
        }
        if (number.right(1) == ".")
        {
            number.append("00");
            return number;
        }
        if (number.right(2).contains(".", Qt::CaseInsensitive))
        {
            number.append("0");
            return number;
        }
    }
    else
    {
        number.append(".00");
        return number;
    }

    return NULL;
}

void HandleMemberCard::on_lineEditMemberCardAmount_editingFinished()
{
    ui->lineEditMemberCardAmount->setText(completionPrice(ui->lineEditMemberCardAmount->text()));
}

void HandleMemberCard::on_lineEditCashPayment_editingFinished()
{
//    if (ui->lineEditMemberCardAmount->text() == "0.00")
//    {
//        ui->lineEditCashPayment->setText("0.00");
//        return;
//    }
    ui->lineEditCashPayment->setText(completionPrice(ui->lineEditCashPayment->text()));
}

void HandleMemberCard::on_lineEditCUPPayment_editingFinished()
{
    ui->lineEditCUPPayment->setText(completionPrice(ui->lineEditCUPPayment->text()));
}

void HandleMemberCard::on_lineEditIntegration_editingFinished()
{
    if (ui->lineEditIntegration->text().isEmpty())
    {
        ui->lineEditIntegration->setText("0");
    }
}

void HandleMemberCard::on_lineEditDiscount_editingFinished()
{
    if (ui->lineEditDiscount->text().isEmpty())
    {
        ui->lineEditDiscount->setText("100");
    }
}

void HandleMemberCard::slotUpdatelineEditHandleDate(QString date)
{
    ui->lineEditHandleDate->setText(date);
}

void HandleMemberCard::on_pushButtonAddMemberLevel_clicked()
{
    m_setMemberLevel->move((QApplication::desktop()->width() - m_setMemberLevel->width())/2 - 300,
                     (QApplication::desktop()->height() - m_setMemberLevel->height())/2);
    m_setMemberLevel->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );
    //在对 QWidget 操作时，不能操作其他东西
    m_setMemberLevel->setWindowModality(Qt::ApplicationModal);
    m_setMemberLevel->setAttribute(Qt::WA_ShowModal, true);
    m_setMemberLevel->displayMemberLevel();
    m_setMemberLevel->exec();
}

void HandleMemberCard::slotUpdateMemberLevel()
{
    ui->comboBoxMemberLevel->clear();
    ui->comboBoxMemberLevel->addItems(m_sql->inquireAllMemberLevelValue());
    ui->comboBoxMemberLevel->setCurrentIndex(-1);
}

void HandleMemberCard::on_comboBoxMemberLevel_currentIndexChanged(int index)
{
    if (index == -1)
    {
        initMemberLevel();
        return;
    }

    const memberList MemberList = m_sql->InquireMemberLevel();
    ui->lineEditIntegration->setText(MemberList.at(index).get_integration());
    ui->lineEditDiscount->setText(MemberList.at(index).get_discount());
    ui->lineEditMemberCardAmount->setText(completionPrice(MemberList.at(index).get_membercardAmount()));
    ui->lineEditCashPayment->setText(completionPrice(ui->lineEditMemberCardAmount->text()));
}
